#define _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"
void SeqListInit(SL*ps)
{
	ps->a = NULL;
	ps->size = 0;
	ps->capicity = 0;
}
void SeqListPrint(SL*ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
void SeqListDestory(SL* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->size = 0;
	ps->capicity = 0;
}
void SeqListCheckCapicity(SL* ps)
{
	if (ps->size == ps->capicity)
	{
		int newcapicity = ps->capicity == 0 ? 4 : ps->capicity * 2;
		SQDataType* tmp = (SQDataType*)realloc(ps->a, newcapicity * sizeof(SQDataType));
		if (tmp == NULL)
		{
			printf("realloc is fail!\n");
			exit(-1);
		}
		else
		{
			ps->a = tmp;
			ps->capicity = newcapicity;
		}
	}
}
void SeqListInsert(SL* ps, int pos, SQDataType x)
{
	assert(ps->size >= pos);
	SeqListCheckCapicity(ps);
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}
void SeqListPushBack(SL* ps, SQDataType x)
{
	SeqListCheckCapicity(ps);
	//ps->a[ps->size] = x;
	//ps->size++;
	SeqListInsert(ps, ps->size,x);
}
void SeqListPushFront(SL* ps,SQDataType x)
{
	//SeqListCheckCapicity(ps);
	//int end = ps->size-1;
	//while (end >= 0)
	//{
	//	ps->a[end + 1] = ps->a[end];
	//	end--;
	//
	//ps->a[0] = x;
	//ps->size++;
	SeqListInsert(ps, 0, x);
}
void SeqListPopFront(SL*ps)
{
	//assert(ps->size > 0);
	//int star = 1;
	//while (star<ps->size)
	//{
	//	ps->a[star - 1] = ps->a[star];
	//	star++;
	//}
	//ps->size--;
	SeqListErase(ps, 0);
}
void SeqListPopBack(SL* ps)
{
	//assert(ps->size > 0);
	//ps->size --;
	SeqListErase(ps, ps->size - 1);
}
void SeqListErase(SL*ps,int pos)
{
	assert(ps->size > pos);
	int star = pos;
	while (star<ps->size-1)
	{
		ps->a[star] = ps->a[star + 1];
		star++;
	}
	ps->size--;
}
int SeqListFind(SL*ps,SQDataType x)
{
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}
void SeqListModity(SL*ps,int pos,SQDataType x)
{
	assert(ps->size > pos);
	ps->a[pos] = x;
}